﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.Reflection;
using QiAdmin.Core;
using QiAdmin.Core.Attributes;
using QiAdmin.Core.Models;
using QiAdmin.Core.Services.Cache;
using QiAdmin.Core.Services.Quartz;
using QiAdmin.Core.Utils;
using QiAdmin.Model.Dtos.Input;
using QiAdmin.Model.System.Models;
using QiAdmin.Service.Service;

namespace QiAdmin.WebApi.Controllers.System
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class SysDataController : ControllerBase
    {
        readonly ICacheService _cacheService;
        public SysDataController(ICacheService cacheService)
        {
            _cacheService = cacheService;
        }

        [Permission(PermissionType.Query)]
        [HttpPost]
        public ApiJsonResult GetDataRang()
        {
            return new ApiJsonResult(App.StaticData.DataRangs);
        }

        [Permission(PermissionType.Query)]
        [HttpPost]
        public ApiJsonResult GetPermissionType()
        {
            return new ApiJsonResult(App.StaticData.PermissionTypes);
        }

        [Permission(PermissionType.Query)]
        [HttpPost]
        public ApiJsonResult GetApiTag()
        {
            return new ApiJsonResult(_cacheService.Get<List<string>>("controllerData"));
        }

        [Permission(PermissionType.System)]
        [HttpPost]
        public ApiJsonResult GetServerInfo()
        {
            ServerInfo serverInfo = new ServerInfo()
            {
                MachineName = ServerInfoUtil.MachineName,
                OSName = ServerInfoUtil.OSName,
                OSArchitecture = ServerInfoUtil.OSArchitecture,
                DoNetName = ServerInfoUtil.DoNetName,
                IP = ServerInfoUtil.IP[0],
                CpuCount = ServerInfoUtil.CpuCount,
                UseRam = ServerInfoUtil.UseRam,
                StartTime = ServerInfoUtil.StartTime,
                RunTime = ServerInfoUtil.RunTime,
                DiskInfo = ServerInfoUtil.DiskInfo,
                MemoryInfo = ServerInfoUtil.MemoryInfo
            };

            return new ApiJsonResult(serverInfo);
        }

        [Permission(PermissionType.Query)]
        [HttpPost]
        public ApiJsonResult GetTaskClass()
        {
            List<string> tasks=new List<string>();

            Assembly assembly = Assembly.Load("QiAdmin.Tasks");
            foreach (var type in assembly.GetTypes())
            {
                if (type.GetInterface(nameof(IBaseTask))!=null)
                {
                    tasks.Add(type.Name);
                }
            }
            return new ApiJsonResult(tasks);
        }
    }
}
